﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NetTopologySuite.Index;
using NetTopologySuite.Index.Quadtree;

namespace NetTopologySuite.Simplify
{
	public class LineSegmentIndex
	{
		public void Add(TaggedLineString line)
		{
			foreach (TaggedLineSegment seg in line.Segments)
			{
				this.Add(seg);
			}
		}

		public void Add(LineSegment seg)
		{
			this._index.Insert(new Envelope(seg.P0, seg.P1), seg);
		}

		public void Remove(LineSegment seg)
		{
			this._index.Remove(new Envelope(seg.P0, seg.P1), seg);
		}

		public IList<LineSegment> Query(LineSegment querySeg)
		{
			Envelope searchEnv = new Envelope(querySeg.P0, querySeg.P1);
			LineSegmentVisitor lineSegmentVisitor = new LineSegmentVisitor(querySeg);
			this._index.Query(searchEnv, lineSegmentVisitor);
			return lineSegmentVisitor.Items;
		}

		private readonly ISpatialIndex<LineSegment> _index = new Quadtree<LineSegment>();
	}
}
